<?php
namespace app\common\model;

use think\Exception;
use think\Model;
use think\Db;
use app\common\model\User as UserModel;

/**
 * 会员余额日志模型
 */
class UserMoneyLog extends Model {
    // 表名
    protected $name = 'user_money_log';
    private $UserModel;


    public function __construct()
    {
        parent::__construct();
        $this->UserModel =  new UserModel();
    }
    public function getLitestoreBy(){
        //liteStoreShopOrder
        return $this->hasOne("litestoreShoporder","shoporder_id","id")->field('shoporder_id,playUser_id');
    }


    /**
     * 用户返余额级插入流水记录
     * @param array $data   数组要插入的
     * @param $user_id      用户id
     * @return bool         操作返回结果
     */
    public function InsertMoneyLog($data=array(),$user_id){
        try{
            Db::startTrans();
            $where['id']=array('eq',$user_id);
            Db::name('user')
                ->where($where)
                ->setInc('money', $data['money']);
            Db::name('user_money_log')->insert($data);
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
            return false;
        }
        return true;
    }

    /**
     * 贡献值返比处理
     * @param array $data 传入数组-流水账表插入数据
     * @param $residual   传入贡献剩余的值
     * @return bool       返回处理真假
     */
    public function SetCore($data=array(),$residual){
        DB::startTrans();
        try{
            $UserUpdata['score']=$data['after'];
            $UserUpdata['residual']=$residual;
            Db::name('user_money_log')->insert($data);
            Db::name('user')->where(['id'=>$data['user_id']])->update($UserUpdata);
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
            return false;
        }
        return true;
    }

    /**
     * 单数组插入流水账表记录
     * @param array $arrayList  传处数组
     * @return bool             数据处理反馈
     */
    public function setMoneyLog($arrayList=array()){
        Db::startTrans();
        try{
            Db::name('user_money_log')->insert($arrayList);
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
            return false;
        }
        return true;
    }

    /**
     * H5端我的余额列表
     *
     * @param $user_id
     * @param $page
     * @return mixed
     */
    public function balanceList($user_id,$page){
        //获取当前登录用户id
        $where['ml.user_id']=array('eq',$user_id);
        //查询类型:奖励,3=服务中心奖励,4=分红奖励,5=股权奖励,6=充值,7=提现,8=贡献值,9=消费,10=货款,11=开通个权,12=消费总金额,13=跨界收益
        $where['ml.money_type']=array('in',array('1','2','3','4','5','7','9','10','11','13'));
        $data['data']=[];
        try{
            $list=Db::name('user_money_log')
                ->alias('ml')
                ->join('litestore_shoporder o','ml.shoporder_id=o.shoporder_id','LEFT')
                ->field('ml.*,o.playUser_id')
                ->where($where)
                ->page($page)
                ->order('ml.createtime desc')
                ->limit(10)
                ->select();
            if(!empty($list)){
                $list=collection($list)->toArray();
                foreach ($list as $key=>$val){
                    //时间戳格式化
                    $list[$key]['createtime']=datetime($val['createtime']);

                    //判断类型如果是11=开通个权,'shoporder_id为来源用户id
                    if($val['money_type']=='11'&&$val['shoporder_id']>0){
                        $list[$key]['playUser_id']=$val['shoporder_id'];
                    }

                    //判断类型如果是7=提现，通过查询提现记录表来查询状态赋值进行显示
                    if($val['money_type']=='7'){
                        $userlai=UserDeposit::get(['moneylog_id'=>$val['id']]);
                        if(!empty($userlai)){
                            if($userlai->status==1){
                                $list[$key]['status']='审核中';
                            }elseif($userlai->status==2){
                                $list[$key]['status']='审核通过';
                            }elseif ($userlai->status==3){
                                $list[$key]['status']='审核失败';
                            }
                        }
                    }
                }
                $data['data']=$list;
            }
        }catch (Exception $e){

        }
        return $data;
    }

    /**
     * H5端我的贡献值列表
     *
     * @param $user_id
     * @param $page
     * @return array
     */
    public function getContributionList($user_id,$page){
        $where['money_type']=array('eq','8');
        //当前登录用户id
        $where['user_id']=array('eq',$user_id);
        $data=array();
        try{
            $data=Db::name('user_money_log')
                ->where($where)
                ->page($page)
                ->limit(10)
                ->order('createtime desc')
                ->select();
            foreach ($data as $key=>&$val){
                $data[$key]['createtime']=datetime($val['createtime']);

                if($val['shoporder_id']){
                    $shoporder=Db::name('litestore_shoporder')->where(['shoporder_id'=>array('eq',$val['shoporder_id'])])->find();
                    if($shoporder){
                        $val['playUser_id']=$shoporder['playUser_id'];
                    }else{
                        $val['playUser_id']=0;
                    }
                }

            }
        }catch (Exception $e){

        }
        return array('data'=>$data);

    }

    /**
     * h5端获取资金变动类型总金额
     *
     * @param $user_id 用户id
     * @param array $money_type 类型array
     * @return float|int
     */
    public function getSum($user_id,$money_type=array()){
        $sum=0;
        try{
            $where['user_id']=array('eq',$user_id);
            $where['money_type']=array('in',$money_type);
            $sum=Db::name('user_money_log')
                ->where($where)
                ->sum('money');
        }catch (Exception $e){

        }
        return $sum;
    }

}